﻿using System.Collections.Generic;
using System.Linq;
using System.Web.Routing;
using Isis.Helpers.Interfaces;

namespace Isis.Helpers.Impl
{
    public class KeyValueListHelper : IKeyValueListHelper
    {
        public string ToKeyValueList(IDictionary<string, object> dictionary)
        {
            if (dictionary == null)
            {
                return "<em>null</em>";
            }
            if (!dictionary.Any())
            {
                return "<em>empty set</em>";
            }
            return string.Join("<br>", dictionary.Select(ToKeyValueItemInList));
        }

        public string ToKeyValueItemInList(KeyValuePair<string, object> pair)
        {
            return string.Format("<strong>{0}</strong> <var>{2}</var><br>{1}<br>", pair.Key, GetDisplayValue(pair.Value), pair.Value.GetType().Name);
        }

        // TODO: move out, add more types...
        public string GetDisplayValue(object value)
        {
            if (value is string)
            {
                return "\"" + value + "\"";
            }
            if (value as HttpMethodConstraint != null)
            {
                return string.Join(", ", (value as HttpMethodConstraint).AllowedMethods);
            }
            return value.ToString();
        }
    }
}